package com.zkk.web;

import com.zkk.pojo.PageUtil;
import com.zkk.pojo.Student;
import com.zkk.service.StudentService;
import com.zkk.service.impl.StudentServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@WebServlet("/findAll")
public class findAllServlet extends HttpServlet {
//    @Override
//    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        StudentService studentService = new StudentServiceImpl();
//        List<Student> list = studentService.findAll();
//        HttpSession session = req.getSession();
//        session.setAttribute("list",list);
//        req.getRequestDispatcher("/showAll.jsp").forward(req,resp);
//    }


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       /* StudentService studentService = new StudentServiceImpl();
        List<Student> list = studentService.findAll();
        HttpSession session = req.getSession();
        session.setAttribute("list",list);
        req.getRequestDispatcher("/showAll.jsp").forward(req,resp);*/
        //上面是查询全部
        String username = req.getParameter("username");
        String currentPage = req.getParameter("currentPage");
        String pageSize = req.getParameter("pageSize");
        //设置默认的当前页面和尺寸
        int cuPage = 0;
        if (currentPage == null || "".equals(currentPage)) {
            cuPage = 1;
        } else {
            cuPage = Integer.valueOf(currentPage);
        }
        int pgSize = 0;
        if (pageSize == null || "".equals(pageSize)) {
            pgSize = 5;
        } else {
            pgSize = Integer.valueOf(pageSize);
        }
        StudentService studentService = new StudentServiceImpl();
        PageUtil<Student> pageUtil = new PageUtil<>();
        pageUtil.setCurrentPage(cuPage);
        pageUtil.setPageSize(pgSize);
        if (username != null && !"".equals(username)) {
            studentService.findAll(pageUtil, username);
        } else {
            pageUtil = studentService.findAll(pageUtil);
        }

        req.getSession().setAttribute("list", pageUtil);
        req.getRequestDispatcher("/showPage.jsp").forward(req, resp);


    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}
